package com.example.spsaveqqty.guides;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.spsaveqqty.R;
import com.example.spsaveqqty.map_weather;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import androidx.viewpager.widget.ViewPager.OnPageChangeListener;
import android.widget.ImageView.ScaleType;

public class StraInfoActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageButton ib_goback;
    private TextView tv_location,tv_userName,tv_title,tv_comment,tv_date;
    private ImageView imageView,iv_headImage;//图片区
    private ViewPager viewPager;
    private ArrayList<View> pageViews;
    private ImageView[] imageViews;
    // 包裹小圆点的LinearLayout
    private ViewGroup group;
    private int i;
    private String location = new String();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置无标题窗口
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_stra_info);
        ib_goback = findViewById(R.id.ib_goback);//返回攻略主页
        ib_goback.setOnClickListener(this);
        tv_location = findViewById(R.id.tv_location);//导航定位
        tv_location.setOnClickListener(this);
        tv_userName = findViewById(R.id.tv_userName);
        tv_title = findViewById(R.id.tv_title);
        tv_comment = findViewById(R.id.tv_comment);
        tv_date = findViewById(R.id.tv_date);
        iv_headImage = findViewById(R.id.iv_headImage);
        //获取Bundle的信息
        Bundle bundle=getIntent().getExtras();
        //设置详情页面各参数
        tv_userName.setText(bundle.getString("producer_name"));
        tv_title.setText(bundle.getString("title"));
        tv_comment.setText(bundle.getString("conent"));
        tv_date.setText(bundle.getString("datetime"));
        tv_location.setText(bundle.getString("place_name"));
        location = bundle.getString("place_name");
        String[] str = bundle.getStringArray("pictures");
        String flag = new String();
        flag = bundle.getString("flag");
        if(!TextUtils.isEmpty(flag)) {
            if (flag.equals("1")) {        //	相册
                Bitmap bitmap = BitmapFactory.decodeFile(bundle.getString("image"));
                bitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
                bitmap = toRoundBitmap(bitmap);//调用自己写的方法把bitmap转成圆形
                iv_headImage.setImageBitmap(bitmap);
            } else if (flag.equals("2")) {    //	拍照
                Uri imgUri = Uri.parse(bundle.getString("image"));
                try {
                    Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imgUri));
                    bitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
                    bitmap = toRoundBitmap(bitmap);//调用自己写的方法把bitmap转成圆形
                    iv_headImage.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        pageViews = new ArrayList<View>();
        i = 0;
        while(str[i] != null) {
            LinearLayout layout = new LinearLayout(this);
            LayoutParams ltp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
            final ImageView imageView = new ImageView(this);
            imageView.setScaleType(ScaleType.FIT_XY);
            Bitmap bp = BitmapFactory.decodeFile(str[i]);
            bp = Bitmap.createScaledBitmap(bp,200,200,true);
            imageView.setImageBitmap(bp);
            layout.addView(imageView,ltp);
            pageViews.add(layout);
            i++;
        }
        imageViews = new ImageView[pageViews.size()];
        group = (ViewGroup) this.findViewById(R.id.viewGroup);
        viewPager = (ViewPager) this.findViewById(R.id.guidePages);

        /**
         * 有几张图片 下面就显示几个小圆点
         */

        for (i = 0; i < pageViews.size(); i++) {
            LinearLayout.LayoutParams margin = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            // 设置每个小圆点距离左边的间距
            margin.setMargins(15, 0, 0, 0);
            imageView = new ImageView(StraInfoActivity.this);
            // 设置每个小圆点的宽高
            imageView.setLayoutParams(new LayoutParams(20, 20));
            imageViews[i] = imageView;
            if (i == 0) {
                // 默认选中第一张图片
                imageViews[i]
                        .setBackgroundResource(R.drawable.page_indicator_focused);
            } else {
                // 其他图片都设置未选中状态
                imageViews[i]
                        .setBackgroundResource(R.drawable.page_indicator_unfocused);
            }
            group.addView(imageViews[i], margin);
        }

        // 给viewpager设置适配器
        viewPager.setAdapter(new GuidePageAdapter());
        // 给viewpager设置监听事件
        viewPager.setOnPageChangeListener(new GuidePageChangeListener());
    }

    // 指引页面数据适配器
    class GuidePageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return pageViews.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getItemPosition(Object object) {
            // TODO Auto-generated method stub
            return super.getItemPosition(object);
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            // TODO Auto-generated method stub
            ((ViewPager) arg0).removeView(pageViews.get(arg1));
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            // TODO Auto-generated method stub
            ((ViewPager) arg0).addView(pageViews.get(arg1));
            return pageViews.get(arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void finishUpdate(View arg0) {
            // TODO Auto-generated method stub

        }
    }

    // 指引页面更改事件监听器
    class GuidePageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int arg0) {
            // 遍历数组让当前选中图片下的小圆点设置颜色
            for (int i = 0; i < imageViews.length; i++) {
                imageViews[arg0]
                        .setBackgroundResource(R.drawable.page_indicator_focused);

                if (arg0 != i) {
                    imageViews[i]
                            .setBackgroundResource(R.drawable.page_indicator_unfocused);
                }
            }
        }
    }
    //设置图片格式
    public Bitmap toRoundBitmap(Bitmap bitmap){
        int width=bitmap.getWidth();
        int height=bitmap.getHeight();
        int r=0;
        //取最短边做边长
        if(width<height){
            r=width;
        }else{
            r=height;
        }
        //构建一个bitmap
        Bitmap backgroundBm=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
        //new一个Canvas，在backgroundBmp上画图
        Canvas canvas=new Canvas(backgroundBm);
        Paint p=new Paint();
        //设置边缘光滑，去掉锯齿
        p.setAntiAlias(true);
        RectF rect=new RectF(0, 0, r, r);
        //通过制定的rect画一个圆角矩形，当圆角X轴方向的半径等于Y轴方向的半径时，
        //且都等于r/2时，画出来的圆角矩形就是圆形
        canvas.drawRoundRect(rect, r/2, r/2, p);
        //设置当两个图形相交时的模式，SRC_IN为取SRC图形相交的部分，多余的将被去掉
        p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        //canvas将bitmap画在backgroundBmp上
        canvas.drawBitmap(bitmap, null, rect, p);
        return backgroundBm;
    }
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ib_goback://返回攻略主界面页面
            {
                Intent intent = new Intent(StraInfoActivity.this, GuidesMainActivity.class);
                startActivity(intent);
                StraInfoActivity.this.finish();
            }
            break;
            case R.id.tv_location://导航
            {
                Intent intent = new Intent();
                intent.putExtra("position_name", location);
                intent.setClass(StraInfoActivity.this, map_weather.class);
                startActivity(intent);
                StraInfoActivity.this.finish();//结束当前activity生命周期
            }
        }
    }
    //手机返回键点击处理
    public void onBackPressed() {
        Intent intent = new Intent(StraInfoActivity.this, GuidesMainActivity.class);
        startActivity(intent);
        StraInfoActivity.this.finish();
    }
}
